<?php
	class AdverAction extends BaseAction{
		private $fileName;
		private $width;//缩略图宽
		private $height;//缩略图高
		private $prefix;//缩略图前缀
		public function index(){
			$adver = M('Adver');
			import("ORG.Util.Page");
			$where = array();
			//广告类型筛选显示
			if(empty($_GET) || 0 == $_GET['kind']){
				$count = $adver->count();
			}else if(!empty($_GET['kind'])){
				switch ($_GET['kind']){
					case 1: 
						$where['type'] = 1;
						break;
					case 2:
						$where['type'] = 2;
						break;
					case 3:
						$where['type'] = 3;
						break;
					default:
						$this->error('查看广告类型错误!');
						exit();
						break;
				}
				$count = $adver->where($where)->count();
				$this->assign('kind',$_GET['kind']);
			}
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $adver->limit($page->firstRow.','.$page->listRows)
						->where($where)
						->field(true)
						->select();
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}
		
		//更新广告js文件
		public function updateJs(){
			switch($_GET['type']){
				case 'text' :
					$this->updateText();
					break;
				case 'header':
					$this->updateHeader();
					break;
				case 'siderbar':
					$this->updateSiderbar();
					break;
				default:break;
			}
		}
		
		//更新文字广告js
		private function updateText(){
			$adver = M('Adver');
			$data = $adver->field(true)
						->where('state = 1 and type = 1')
						->order('date desc')
						->limit(C('TEXT_ADVER_NUM'))
						->select();
			$js = "var text = [];\r\n";
			$i = 0;
			if ($data) {
				foreach ($data as $val) {
					$i++;
					$js .= "text[$i] = {\r\n";
					$js .= 	"\t'title' : '".$val['title']."',\r\n";
					$js .= 	"\t'link' : '".$val['link']."'\r\n";
					$js .= "};\r\n";
				}
			}
			$js .= "var i = Math.floor(Math.random()*$i+1);\r\n";
			$js .= "document.write('<a href=\"'+text[i].link+'\" class=\"adv\" target=\"_blank\">'+text[i].title+'</a>');\r\n";
			if (!file_put_contents('./Public/Js/text_adver.js', $js)) {
				$this->error('更新文字广告js失败！');
				exit();
			}
			$this->success('更新文字广告js成功！');
		}
		
		//更新头部图片广告js
		private function updateHeader(){
			$adver = M('Adver');
			$data = $adver->field(true)
						->where('state = 1 and type = 2')
						->order('date desc')
						->limit(C('HEADER_ADVER_NUM'))
						->select();
			$js = "var header = [];\r\n";
			$i = 0;
			if ($data) {
				foreach ($data as $val) {
					$i++;
					$js .= "header[$i] = {\r\n";
					$js .= 	"\t'title' : '".$val['title']."',\r\n";
					$js .= 	"\t'pic' : '".C('WEB_DIR').'/Public/Uploads/header_'.$val['thumbnail']."',\r\n";
					$js .= 	"\t'link' : '".$val['link']."'\r\n";
					$js .= "};\r\n";
				}
			}
			$js .= "var i = Math.floor(Math.random()*$i+1);\r\n";
			$js .= "document.write('<a href=\"'+header[i].link+'\" target=\"_blank\" title=\"'+header[i].title+'\"><img src=\"'+header[i].pic+'\"></a>');";
			if (!file_put_contents('./Public/Js/header_adver.js', $js)) {
				$this->error('更新头部图片广告js失败！');
				exit();
			}
			$this->success('更新头部图片广告js成功！');
		}
		
		//更新侧边栏图片广告js
		private function updateSiderbar(){
			$adver = M('Adver');
			$data = $adver->field(true)
						->where('state = 1 and type = 3')
						->order('date desc')
						->limit(C('SIDERBAR_ADVER_NUM'))
						->select();
			$js = "var siderbar = [];\r\n";
			$i = 0;
			if ($data) {
				foreach ($data as $val) {
					$i++;
					$js .= "siderbar[$i] = {\r\n";
					$js .= 	"\t'title' : '".$val['title']."',\r\n";
					$js .= 	"\t'pic' : '".C('WEB_DIR').'/Public/Uploads/siderbar_'.$val['thumbnail']."',\r\n";
					$js .= 	"\t'link' : '".$val['link']."'\r\n";
					$js .= "};\r\n";
				}
			}
			$js .= "var i = Math.floor(Math.random()*$i+1);\r\n";
			$js .= "document.write('<a href=\"'+siderbar[i].link+'\" target=\"_blank\" title=\"'+siderbar[i].title+'\"><img src=\"'+siderbar[i].pic+'\"></a>');";
			if (!file_put_contents('./Public/Js/siderbar_adver.js', $js)) {
				$this->error('更新侧边栏图片广告js失败！');
				exit();
			}
			$this->success('更新侧边栏图片广告js成功！');
		}
		
		//通过显示
		public function pass(){
			if(empty($_GET['id'])){
				$this->error('请选择要显示的广告！');
				exit();
			}
			$adver = M('Adver');
			$adverData = $adver->getById($_GET['id']);
			if(!$adverData){
				$this->error('请确保要显示的广告ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 1;
			if($adver->where('id = '.$_GET['id'])->save($data)){
				$this->success('显示成功！');
			}else{
				$this->error('显示失败！');
			}
		
		}
		
		//取消显示
		public function cancel(){
			if(empty($_GET['id'])){
				$this->error('请选择要取消显示的广告！');
				exit();
			}
			$adver = M('Adver');
			$adverData = $adver->getById($_GET['id']);
			if(!$adverData){
				$this->error('请确保要取消显示的广告ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 0;
			if($adver->where('id = '.$_GET['id'])->save($data)){
				$this->success('取消显示成功！');
			}else{
				$this->error('取消显示失败！');
			}
		}
		
		public function add(){
			$this->display();
		}
		
		public function insert(){
			$adver = D('Adver');
			if($data = $adver->create())
			{
				if($adver->add())
				{
					$this->assign("jumpUrl","__URL__");
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($adver->getError());
			}
		}
		
		public function edit()
		{
			if(!empty($_GET['id']))
			{
				$adver = M('Adver');
				$data = $adver->getById($_GET['id']);
				// 				var_dump($data);
				if(null == $data)
					$this->error("请确认要修改的广告ID合法！");
				$this->assign('adver',$data);
				$this->display();
		
			}else{
				$this->error("请选择要修改的广告！");
			}
		}
		
		public function update()
		{
			$adver = D('Adver');
			if($adver->create()){
				if($adver->save()){
					$this->success("更新成功！");
				}else{
					$this->error("因为数据没变化或更新失败！");
				}
			}else{
				$this->error($adver->getError());
			}
		
		}
		
		public function delete()
		{
			if(!empty($_GET['id']))
			{
				$adver = M('Adver');
				if($adver->delete($_GET['id'])){
					$this->success("删除广告成功!");
				}else{
					$this->error("删除广告失败!");
				}
			}else{
				$this->error("请选择要删除的广告！");
			}
		}
		
		
		
		//通过js调用这个页面，用于选择文件上传
		public function showUploadFile(){
			if('header' == $_GET['type']){
				$this->assign('width',690);
				$this->assign('height',80);
			}else if('siderbar' == $_GET['type']){
				$this->assign('width',270);
				$this->assign('height',200);
			}
			$this->assign('prefix',$_GET['type']);
			$this->display();
		}

		//接受上传操作，并将上传结果返回，取得上传路径并显示上传图片
		public function upload(){
			// 			dump($_FILES);
			// 			exit();
			$this->fileName = md5_file($_FILES['pic']['tmp_name']);
			$this->width = $_POST['width'];
			$this->height = $_POST['height'];
			$this->prefix = $_POST['prefix'].'_';
			if(!empty($_FILES)){
				$result = $this->_upload();
				if(isset($result)){
					$path = C('WEB_DIR').'/Public/Uploads/'.$this->prefix.$result[0]['savename'];
					$name = $result[0]['savename'];
					echo "<script type='text/javascript'>opener.document.content.thumbnail.value='$name';</script>";
					echo "<script type='text/javascript'>opener.document.content.pic.style.display='block';</script>";
					echo "<script type='text/javascript'>opener.document.content.pic.src='$path';</script>";
					$this->success('上传文件成功！');
					echo "<script type='text/javascript'>window.close();</script>";
					exit();

				}else{
					$this->error('上传文件失败！');
				}
			}else{
				$this->error('请选择要上传的文件！');
			}
		}

		//处理上传过程
		private function _upload(){
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();
			$upload->maxSize = 204800;//上传文件最大大小
			$upload->savePath = './Public/Uploads/';//上传文件保存路径
			$upload->saveRule = $this->fileName;//设置文件名保存规则
			$upload->allowExts=array('jpg','jpeg','png','gif');//设置允许的文件后缀
			$upload->allowTypes=array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg','image/x-png','image/x-jpg','image/x-pjpeg','image/x-gif','image/x-jpeg');  //检测mime类型
			$upload->thumb=true;   //是否开启图片文件缩略
			$upload->thumbMaxWidth=$this->width;  //以字串格式来传，如果你希望有多个，那就在此处，用,分格，写上多个最大宽
			$upload->thumbMaxHeight=$this->height;	//最大高度
			$upload->thumbPrefix=$this->prefix;//缩略图文件前缀
			$upload->uploadReplace=true;
			$upload->thumbRemoveOrigin=1;  //如果生成缩略图，是否删除原图
			if($upload->upload()){
				return $upload->getUploadFileInfo();
			}else{
				//echo $upload->getErrorMsg();
				$this->error($upload->getErrorMsg());
			}
		}
	}

?>